<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class TempCommand extends Command
{
    //命令名
    protected $signature = 'route:add';

    //命令描述
    protected $description = '获取所有路由并添加到表操作';

    public function __construct()
    {
        parent::__construct();
    }

    //业务处理
    public function handle()
    {
        $data = \Route::getRoutes();
        foreach ($data as $k => $v) {
            if (!isset($v['action']['uses'])){
                continue;
            }
            if (!in_array('api', $v['action']['middleware'])){
                continue;
            }
            $dir = $v['action']['uses'];
            $dirArr = explode('@',$dir);
            $ctrDir = trim(str_replace("App\Http\Controllers\Admin", '',$dirArr[0]), "\\\\");
            $ctrDirArr = explode('\\',$ctrDir);

            if (count($dirArr) < 2) {
                continue;
            }
            if (count($ctrDirArr) > 1) {
                $ctr_dir = $ctrDirArr[0];//控制器所在目录，为空表示直接在AppHttpControllersAdmin目录下
                $ctr = $ctrDirArr[1];//控制器
            } else {
                $ctr_dir = '';//控制器所在目录，为空表示直接在AppHttpControllersAdmin目录下
                $ctr = $ctrDirArr[0];//控制器

            }
            $action = $dirArr[1];//方法
            $uri = trim($v['uri'], '/');//路由地址
            $method = strtolower($v['method']);//请求方式
            $insetData[] = [
                'ctr_dir' => $ctr_dir,
                'ctr' => $ctr,
                'action' => $action,
                'uri' => $uri,
                'method' => $method,
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];

        }
        app('model_route')->insert($insetData);

        dump('路由操作已完成');
    }
}
